﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Muel.TypeScriptFacade.Core
{
    public class TypeScriptEnum : TypeScriptType
    {
        public TypeScriptEnum(TypeScriptServiceManager manager, Type type) : base(manager, type) { }

        /// <summary>
        /// {0}: Enum name
        /// {1}: Enum values
        /// </summary>
        private const string enumFormat = @"
    export enum {0} {{
        {1}
    }}";

        internal override string ToTypeScript()
        {
            // build a list of the possible enumeration names and values
            string[] names = type.GetEnumNames();
            string[] nameValues = names.Select(name => string.Format("{0} = {1}", name, (int)Enum.Parse(type, name))).ToArray();

            // comma separate the name-value list and return in the enum format
            return string.Format(enumFormat, type.Name, string.Join("," + Environment.NewLine, nameValues));
        }

        internal override void InitialiseInheritance()
        {
            // no inheritance for enums
            this.BaseType = null;
            this.ChildTypes = new TypeScriptType[0];
        }

        public override bool CanSnapshot
        {
            get { return true; }
        }
    }
}
